/*
  SPIMCP.h : customized SPI library for controlling X and Y mirrors, as well as red laser "mean" power
  Configured for Arduino Mega, for use with the MCP4922 Analog to Digital Converter
*/

#ifndef Spimcp_h
#define Spimcp_h

#include "WProgram.h"

// NOTE: The ethernet shield uses pin 10 to select the W5100 chip, and pin4 for the SD card, so it should be ok to use the same clock, miso, mosi pins for the SPI.. but the problem
// is that the Ethernet routines may use interruptions, and then interrupt the output to the mirror DAC??
// -----> AFTER testing, it looks like there is no interference (not clear why though...). It suffices to have a different CS then. 

#define SCK_PIN   52 //SPI Clock
#define MISO_PIN  50 //SPI output (data going to DAC)
#define MOSI_PIN  51 //SPI input (data comming from DAC, here not connected) 

// VERY IMPORTANT: the chip select for the DACs should be different from the default SPI "SS" pin (Slave Select), which is 53 by default (and will be used by the Ethernet Shield). 
#define SS1_PIN   37 //Chip Select of the first DAC (mirrors)
#define SS2_PIN   36 //Chip Select of the second DAC (red Laser)
// Also, IT IS VERY IMPORANT TO CORRECTLY SET THE PORT VALUE when setting the CS to high or low, if not using digitalWrite!!! 
// For instance, on the ArduinoMEGA, digitalWrite(53, LOW) is equivalent but much slower than this: PORTB &= B11111110;
// In fact, on the MEGA we have: 
// PORTA: 22 to 29 
// PORTB: 0,1,2,3 to pins 53, 52, 51 and 50. 
// PORTC: 30 to 37  (PC0 is 37, PC6 is 30) <----- WE WILL USE first two bits of PORTC

class SPIMCP
{
  public:
    SPIMCP(void);
    void init();
    void writeOutX(int value);
    void writeOutY(int value);
    void writeOutR(int value);
	
		
  private:
    void mode(byte);
    byte transfer(byte);
    byte transfer(byte, byte);
    void transfer16Bit(byte, byte);
};

extern SPIMCP Spimcp;

#endif
